package poketrade;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import poketrade.entities.User;
import poketrade.managers.DealManager;

/**
 * Servlet implementation class MakeDealServlet
 */
@WebServlet("/MakeDealServlet")
public class MakeDealServlet extends BaseHttpServlet {
	private static final long serialVersionUID = 1L;
    
	@EJB
	private DealManager mgr;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		long id;
		try {
			 id = Long.parseLong(request.getParameter("id"));
		}
		catch (NumberFormatException e)
		{
			showMessage(request, response, "Offre invalide");
			return;
		}
		
		HttpSession s = request.getSession();
		User currentUser = (User) s.getAttribute("user");
		currentUser = mgr.concludeDeal(id, currentUser.getId());
		
		if (currentUser == null)
			showMessage(request, response, "Pas assez pour acheter cette carte :(");
		else
		{
			s.setAttribute("user", currentUser);
			redirect(response, "PDFGenerator?id="+id);
			
		}
	}

}
